/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System.IO;
using System.Text;
using System.Reflection;
using System.Globalization;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// All common functions used in the class libraries of ExcelQuicker
	/// </summary>
	public sealed class EQKit
	{
		private EQKit(){}


		private static NumberFormatInfo mObjNumberFormatInfo = new NumberFormatInfo();

		/// <summary>
		/// <br>Set/Get NumberFormatInfo object</br>
		/// </summary>
		public static NumberFormatInfo ObjNumberFormatInfo
		{
			get
			{
				return mObjNumberFormatInfo;
			}
			set
			{
				EQKit.mObjNumberFormatInfo = value;
			}
		}


		/// <summary>
		/// Check the position setting, allowable value is between 1 and 65535
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static int CheckPosition(int v)
		{
			if(v>=65535 || v<=0)
				throw new EQException("EQ1007", v.ToString(EQKit.ObjNumberFormatInfo));

			return v;
		}


		/// <summary>
		/// Check the height or width of graph, allowable value is between 1 and 1024
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static float CheckMeasure(float v)
		{
			if(v>=1024 || v<=0)
				throw new EQException("EQ1008", v.ToString(EQKit.ObjNumberFormatInfo));

			return v;
		}


		/// <summary>
		/// Check the width of cell, unknown unit, decided by excel. Allowable value is between 1 and 255
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static int CheckRangeWidth(int v)
		{
			if(v>255 || v<=0)
				throw new EQException("EQ1012", v.ToString(EQKit.ObjNumberFormatInfo));

			return v;
		}


		/// <summary>
		/// Check the height of cell, unknown unit, decided by excel. Allowable value is between 1 and 409
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static int CheckRangeHeight(int v)
		{
			if(v>409 || v<=0)
				throw new EQException("EQ1013", v.ToString(EQKit.ObjNumberFormatInfo));

			return v;
		}


		/// <summary>
		/// Check the size of font, allowable size is between 8 and 72
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static int CheckFontSize(int v)
		{
			if(v>72 || v<8)
				throw new EQException("EQ1014", v.ToString(EQKit.ObjNumberFormatInfo));
			return v;
		}


		/// <summary>
		/// Check the range of color index, allowable value is between 1 and 56
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static int CheckColorIndex(int v)
		{
			if(v>56 || v<1)
				throw new EQException("EQ1011", v.ToString(EQKit.ObjNumberFormatInfo));

			return v;
		}


		/// <summary>
		/// Convert EQBool enum to bool
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static bool ToBool(EQBool v)
		{
			return (v == EQBool.True) ? true : false;
		}


		/// <summary>
		/// Convert bool to EQBool enum
		/// </summary>
		/// <param name="v"></param>
		/// <returns></returns>
		public static EQBool ToEQBool(bool v)
		{
			return (v)?EQBool.True:EQBool.False;
		}


		/// <summary>
		/// <br>If the file name exists, the function will change the file name tinily and return modified file name,but won't change its suffix name.</br>
		/// <br>Example: If the C:\excel20050401.xls exists, the function will return C:\excel200504011.xls</br>
		/// </summary>
		/// <param name="fileName">Physical full file name</param>
		/// <returns></returns>
		public static string ChangeDuplicateFileName(string fileName)
		{
			if(!File.Exists(fileName))
				return fileName;
			else
			{
				string strSubfixFileName = Path.GetExtension(fileName);
				StringBuilder sbCurrectFileName = new StringBuilder();
				sbCurrectFileName.Append(fileName.Substring(0,fileName.Length-strSubfixFileName.Length));
				int i = 2;
				while(true)
				{
					sbCurrectFileName.Append(i.ToString(EQKit.ObjNumberFormatInfo));
					sbCurrectFileName.Append(strSubfixFileName);
					string strCurrectFileName = sbCurrectFileName.ToString();
					if (File.Exists(strCurrectFileName))
					{
						i++;
						sbCurrectFileName.Length = 0;
						sbCurrectFileName.Append(strCurrectFileName.Substring(0, strCurrectFileName.Length-strSubfixFileName.Length));
					}
					else
						break;
				}
				return sbCurrectFileName.ToString();
			}
		}


		/// <summary>
		/// Change the file subfix name to param extension. 
		/// </summary>
		/// <param name="fileName"></param>
		/// <param name="extension"></param>
		/// <returns></returns>
		/// <example>
		/// <br>string strOriginalFileName = @"c:\windows\readme.txt";</br>
		/// <br>string strFileName = EQKit.ChangeSubfix(strOriginalFileName, ".xls");</br>
		/// <br>//As result, the strFileName is c:\windows\readme.xls.</br>
		/// </example>
		public static string ChangeSubfix(string fileName, string extension)
		{
			string strFileName = Path.GetFileNameWithoutExtension(fileName) + extension;
			string strPath = Path.GetDirectoryName(fileName) + "\\";
			return strPath + strFileName;
		}

		/// <summary>
		/// Concat the string array, split each one with parameter "split"
		/// </summary>
		/// <param name="arr"></param>
		/// <param name="split"></param>
		/// <returns></returns>
		public static string Concat(string[] arr, string split)
		{
			StringBuilder sb = new StringBuilder();
			foreach(string str in arr)
				sb.Append(str + split);
			int length = split.Length;
			return sb.ToString().Substring(0, sb.Length - split.Length);
		}

		/// <summary>
		/// Concat the string array, split each one with ","
		/// </summary>
		/// <param name="arr"></param>
		/// <returns></returns>
		public static string Concat(string[] arr)
		{
			return EQKit.Concat(arr, ",");
		}

		/// <summary>
		/// Convert extension of file name based on EQReportFormat enum. etc, if the format is EQReportFormat.Excel and fileName is c:\mywebpage.htm, this function will return c:\mywebpage.xls
		/// </summary>
		/// <param name="fileName"></param>
		/// <param name="format"></param>
		/// <returns></returns>
		public static string ConvertFileName(string fileName, EQReportFormat format)
		{
			switch (format)
			{
				case EQReportFormat.Excel:
					return EQKit.ChangeSubfix(fileName, ".xls");
				case EQReportFormat.HTML:
					return EQKit.ChangeSubfix(fileName, ".htm");
				case EQReportFormat.Spreadsheet:
					return Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + "OWC.htm";
				default:
					return EQKit.ChangeSubfix(fileName, ".xls");
			}
		}

		/// <summary>
		/// Return full file name. If param fileName is only a file name without its path, this function will concat this file with a default path (depended on EQConfiguration.OutputFilePath).
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public static string GetFullFileName(string fileName)
		{
			if(fileName != null || Path.GetDirectoryName(fileName) == "")
				return EQConfiguration.OutputFilePath + fileName;
			else
				return fileName;
		}
	}
}
